import axios from "axios";
import storage from "store";
import router from "@/router";
import store from "@/store"
import { VueAxios } from "./axios";
import { ACCESS_TOKEN } from "./constants";
import notification from "ant-design-vue/es/notification";
import i18n from "@/locale";

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: process.env.VUE_APP_API_BASE_URL,
  timeout: 12000, // 请求超时时间
});

// 异常拦截处理器
const errorHandler = (error) => {
  if (error.response) {
    const data = error.response.data;
    // 从 localstorage 获取 token
    // const token = storage.get(ACCESS_TOKEN)
    if (
      error.response.status === 401 &&
      !(data.result && data.result.isLogin)
    ) {
      
      notification.error({
        message: i18n.t("错误"),
        description: i18n.t("登录已失效"),
      });
      // if (token) {
        store.dispatch('Logout').then(() => {
        setTimeout(() => {
          router.replace({ name: "Login" });
          // window.location.reload();
        }, 1500);
      });
      // }
    }
  }
  return Promise.reject(error);
};

// request interceptor
request.interceptors.request.use((config) => {
  const token = storage.get(ACCESS_TOKEN);
  config.headers['lang'] = localStorage.getItem("fx-lang") || 'zh'
  // if(['/get/support','/user/login'].includes(config.url)){
  //   config.baseURL = process.env.VUE_APP_API_SERVICE_URL
  // }
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (token) {
    config.headers["token"] = token;
  }
  if (config.data && config.data.pageNo) {
    config.data.current = config.data.pageNo;
  }
  console.log(config, process.env.VUE_APP_API_SERVICE_URL,'user/login');
  
  return config;
}, errorHandler);

// response interceptor
request.interceptors.response.use((response) => {
  if (response.data.data && response.data.data.records) {
    response.data.data.totalCount = response.data.data.total;
    response.data.data.pageSize = response.data.data.size;
    response.data.data.pageNo = response.data.data.current;
    response.data.data.totalPage =
      response.data.data.total / response.data.data.size;
    response.data.data.data = response.data.data.records;
  }
  if (response.data.code === "0000") {
    return response.data;
  } else {
    notification.error({
      message: i18n.t("错误"),
      description: response.data.msg || i18n.t("系统错误"),
    });
    return Promise.reject(response.data.msg);
  }
}, errorHandler);

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, request);
  },
};

export default request;

export { installer as VueAxios, request as axios };
